﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace _04_ObserverModel_Basic
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteSubject sub = new ConcreteSubject();
            ConcreteObserver a = new ConcreteObserver("A",sub);
            ConcreteObserver b = new ConcreteObserver("B",sub);
            sub.Attach(a);
            sub.Attach(b);
            sub.SubjectState = "新状态";
            sub.Notiy();

        }
    }

    abstract class Subject
    {
        private IList<Observer> observerList = new List<Observer>();
        public void Attach(Observer observer)
        {
            observerList.Add(observer);
        }
        public void Detach(Observer observer)
        {
            observerList.Remove(observer);
        }
        public void Notiy()
        {
            foreach (Observer item in observerList)
            {
                item.Update();
            }
        }
    }

    class ConcreteSubject : Subject
    {
        public string SubjectState { get; set; }
    }

    abstract class Observer
    {
        public abstract void Update();
    }

    class ConcreteObserver : Observer
    {
        private string name;
        private ConcreteSubject sub;
        public ConcreteObserver(string name,ConcreteSubject sub)
        {
            this.name = name;
            this.sub = sub;
        }
        public override void Update()
        {
            Console.WriteLine($"{name}观察者的状态是{sub.SubjectState}！");
        }
    }
}
